package com.greateffect.littlebud.lib.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import com.greateffect.littlebud.lib.R;
import com.greateffect.littlebud.lib.utils.JColors;
import com.greateffect.littlebud.lib.utils.JLogUtil;
import com.zcs.autolayout.utils.AutoUtils;

/**
 * Created by ZengCS on 2019/8/26.
 * E-mail:zcs@sxw.cn
 * Add:成都市天府软件园E3-3F
 */
public class CustomRoundBg extends View implements GestureDetector.OnGestureListener {
    private static final String TAG = "CustomRoundBg";
    private int mRadius;
    private int mColor;
    private Paint mPaint, mFilterPaint;
    private RectF mRect;
    private boolean mWithFilter = false;

    public void setRadius(int mRadius) {
        this.mRadius = mRadius;
        invalidate();
    }

    public void setRoundBgColor(int mColor) {
        this.mColor = mColor;
        mPaint.setColor(mColor);
        invalidate();
    }

    public CustomRoundBg(Context context) {
        this(context, null);
    }

    public CustomRoundBg(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomRoundBg(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mGestureDetector = new GestureDetector(context, this);

        // 读取配置
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomRoundBg);
        mRadius = array.getDimensionPixelOffset(R.styleable.CustomRoundBg_crb_radius, 0);
        mColor = array.getColor(R.styleable.CustomRoundBg_crb_color, JColors.colorPrimary);
        if (mRadius > 0) {
            mRadius = AutoUtils.getPercentWidthSize(mRadius);
        }
        array.recycle();
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mRect.right = getMeasuredWidth();
        mRect.bottom = getMeasuredHeight();
    }

    private void init() {
        mRect = new RectF(0, 0, 0, 0);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mColor);

        mFilterPaint = new Paint();
        mFilterPaint.setAntiAlias(true);
        mFilterPaint.setStyle(Paint.Style.FILL);
        mFilterPaint.setColor(0x30000000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 每次画的时候，先清空上次的内容
        canvas.drawColor(Color.TRANSPARENT);
        // 1.画背景
        canvas.drawRoundRect(mRect, mRadius, mRadius, mPaint);
        // 2.画点击效果
        if (mWithFilter) {
            canvas.drawRoundRect(mRect, mRadius, mRadius, mFilterPaint);
        }
    }

    /**
     * 监听手势
     */
    private GestureDetector mGestureDetector;

    /**
     * 设置滤镜
     */
    public void setFilter() {
        mWithFilter = true;
        addScale();
        invalidate();
    }

    /**
     * 清除滤镜
     */
    private void removeFilter() {
        mWithFilter = false;
        removeScale();
        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //在cancel里将滤镜取消，注意不要捕获cacncel事件,mGestureDetector里有对cancel的捕获操作
        //在滑动GridView时，AbsListView会拦截掉Move和UP事件，直接给子控件返回Cancel
        if (isClickable() && isFocusable()) {
            JLogUtil.d(TAG, "event.getActionMasked() = " + event.getActionMasked());

            if (event.getAction() == MotionEvent.ACTION_UP && event.getActionMasked() == MotionEvent.ACTION_UP) {
                // 无论什么时候弹起，都触发单击事件
                onSingleTapUp(event);
            }
            if (event.getAction() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
                removeFilter();
            }
        }
        return mGestureDetector.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        if (isClickable() && isFocusable()) {
            setFilter();
        }
        //这里必须返回true，表示捕获本次touch事件
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        if (isClickable() && isFocusable() && mWithFilter) {
            removeFilter();
            performClick();
        }
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        //长按时，手动触发长安事件
        performLongClick();
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }

    private void addScale() {
//        setScaleX(0.94f);
//        setScaleY(0.94f);
    }

    private void removeScale() {
//        setScaleX(1f);
//        setScaleY(1f);
    }
}
